package jp.co.hcs.attendancemanagement.common;

import java.sql.SQLException;

import jp.co.hcs.attendancemanagement.AMConstants;
import jp.co.hcs.attendancemanagement.data.dao.ApprovalDAO;
import jp.co.hcs.attendancemanagement.data.dao.LeaveApproveDAO;
import jp.co.hcs.attendancemanagement.data.dao.WorkingDayApproveDAO;
import jp.co.hcs.attendancemanagement.data.dao.WorkingMonthApproveDAO;
import jp.co.hcs.attendancemanagement.data.entity.Approval;
import jp.co.hcs.attendancemanagement.data.entity.LeaveApprove;
import jp.co.hcs.attendancemanagement.data.entity.WorkingDayApprove;
import jp.co.hcs.attendancemanagement.data.entity.WorkingMonthApprove;
import jp.co.hcs.attendancemanagement.logic.LogicBase;

/**
 * The Class ApprovalRecordRegister.
 */
public class ApprovalRecordRegister extends LogicBase {

  /**
   * Register approval.
   * 
   * @param inputData the input data
   * @return the int
   */
  public int registerApproval(ApprovalRecordData inputData) {

    // approval decision
    ApprovalDAO approveDao = daoFactory.getApprovalDAO();
    try {
      Approval approval = approveDao.selectApprovalByPrimaryKey(inputData.getApprovalTypeId());
      if (approval == null) {
        // resultCode = 1
        return AMConstants.ERROR_FINISH;
      }
      if (!approval.getIsApprovalNeeded()) {
        // resultCode = 2
        return AMConstants.NO_NEED_APPROVE;
      }
    } catch (SQLException e) {
      // resultCode = 1
      return AMConstants.ERROR_FINISH;
    }

    // get approval group id
    ApprovalGroupGetting groupGetting = new ApprovalGroupGetting();
    String approvalGroupId = null;
    try {
      approvalGroupId =
          groupGetting.retrieveApprovalGroupId(inputData.getEmployeeId(), inputData
              .getApprovalTypeId());
      if (approvalGroupId == null) {
        // resultCode = 1;
        return AMConstants.ERROR_FINISH;
      }
    } catch (SQLException e) {
      // resultCode = 1;
      return AMConstants.ERROR_FINISH;
    }

    // process register/edit
    if (AMConstants.APPROVAL_TYPE_DAILY.equals(inputData.getApprovalTypeId())) {
      // process on WorkingDayApprove (TB5_NICHIJI_SHONIN)
      WorkingDayApproveDAO workDayDao = daoFactory.getWorkingDayApproveDAO();
      WorkingDayApprove workingDay = null;
      try {
        workingDay =
            workDayDao.selectWorkingDayApproveByPrimaryKey(inputData.getEmployeeId(), inputData
                .getTargetDay(), inputData.getTargetYearMonth());
      } catch (SQLException e) {
        // do nothing
      }

      if (workingDay == null) {
        // insert new working day
        workingDay = new WorkingDayApprove();
        workingDay.setEmployeeId(inputData.getEmployeeId());
        workingDay.setActualWorkingYearMonth(inputData.getTargetYearMonth());
        workingDay.setActualWorkingDay(inputData.getTargetDay());
        workingDay.setApprovalGroup(approvalGroupId);
        workingDay.setApprovalSteps(new Short("0"));
        if (inputData.getTimingRegistCode() == 1) {
          workingDay.setIsTimingRegistered(true);
        } else if (inputData.getTimingRegistCode() == 2) {
          workingDay.setIsTimingRegistered(false);
        }
        if (inputData.getDayRegistCode() == 1) {
          workingDay.setIsDailyWorkingRegistered(true);
        } else if (inputData.getDayRegistCode() == 2) {
          workingDay.setIsDailyWorkingRegistered(false);
        }
        if (inputData.getEffortRegistCode() == 1) {
          workingDay.setIsDailyEffortRegistered(true);
        } else if (inputData.getEffortRegistCode() == 2) {
          workingDay.setIsDailyEffortRegistered(false);
        }
        try {
          workDayDao.insertWorkingDayApprove(workingDay);
          // resultCode = 0
          return AMConstants.NORMAL_FINISH;
        } catch (SQLException e) {
          // resultCode = 1
          return AMConstants.ERROR_FINISH;
        }
      } else {
        // update working day
        workingDay.setApprovalGroup(approvalGroupId);
        workingDay.setApprovalSteps(new Short("0"));
        if (inputData.getTimingRegistCode() == 1) {
          workingDay.setIsTimingRegistered(true);
        } else if (inputData.getTimingRegistCode() == 2) {
          workingDay.setIsTimingRegistered(false);
        }
        if (inputData.getDayRegistCode() == 1) {
          workingDay.setIsDailyWorkingRegistered(true);
        } else if (inputData.getDayRegistCode() == 2) {
          workingDay.setIsDailyWorkingRegistered(false);
        }
        if (inputData.getEffortRegistCode() == 1) {
          workingDay.setIsDailyEffortRegistered(true);
        } else if (inputData.getEffortRegistCode() == 2) {
          workingDay.setIsDailyEffortRegistered(false);
        }
        try {
          workDayDao.updateWorkingDayApproveByPrimaryKey(workingDay);
          // resultCode = 0
          return AMConstants.NORMAL_FINISH;
        } catch (SQLException e) {
          // resultCode = 1
          return AMConstants.ERROR_FINISH;
        }
      }
    } else if (AMConstants.APPROVAL_TYPE_MONTHLY.equals(inputData.getApprovalTypeId())) {
      // process on WorkingMonthApprove (TB5_GETSUJI_SHONIN)
      WorkingMonthApproveDAO workMonthDao = daoFactory.getWorkingMonthApproveDAO();
      WorkingMonthApprove workingMonth = null;
      try {
        workingMonth =
            workMonthDao.selectWorkingMonthApproveByPrimaryKey(inputData.getEmployeeId(), inputData
                .getTargetYearMonth());
      } catch (SQLException e) {
        // do nothing
      }

      if (workingMonth == null) {
        // insert new working month
        workingMonth = new WorkingMonthApprove();
        workingMonth.setEmployeeId(inputData.getEmployeeId());
        workingMonth.setActualWorkingYearMonth(inputData.getTargetYearMonth());
        workingMonth.setApprovalGroup(approvalGroupId);
        workingMonth.setApprovalSteps(new Short("0"));
        try {
          workMonthDao.insertWorkingMonthApprove(workingMonth);
          // resultCode = 0
          return AMConstants.NORMAL_FINISH;
        } catch (SQLException e) {
          // resultCode = 1
          return AMConstants.ERROR_FINISH;
        }
      } else {
        // update working month
        workingMonth.setApprovalGroup(approvalGroupId);
        workingMonth.setApprovalSteps(new Short("0"));
        try {
          workMonthDao.updateWorkingMonthApproveByPrimaryKey(workingMonth);
          // resultCode = 0
          return AMConstants.NORMAL_FINISH;
        } catch (SQLException e) {
          // resultCode = 1
          return AMConstants.ERROR_FINISH;
        }
      }
    } else {
      // process on LeaveApprove (TB5_SHINSEI_SHONIN)
      LeaveApproveDAO leaveAppDao = daoFactory.getLeaveApproveDAO();
      LeaveApprove leave = null;
      try {
        leave = leaveAppDao.selectLeaveApproveByPrimaryKey(inputData.getApplicationNo());
      } catch (SQLException e) {
        // do nothing
      }

      if (leave == null) {
        // insert new leave approve
        leave = new LeaveApprove();
        leave.setApplicationNo(inputData.getApplicationNo());
        leave.setEmployeeId(inputData.getEmployeeId());
        leave.setApprovalGroup(approvalGroupId);
        leave.setApprovalSteps(new Short("0"));
        try {
          leaveAppDao.insertLeaveApprove(leave);
          // resultCode = 0
          return AMConstants.NORMAL_FINISH;
        } catch (SQLException e) {
          // resultCode = 1
          return AMConstants.ERROR_FINISH;
        }
      } else {
        // update leave approve
        leave.setEmployeeId(inputData.getEmployeeId());
        leave.setApprovalGroup(approvalGroupId);
        leave.setApprovalSteps(new Short("0"));
        try {
          leaveAppDao.updateLeaveApproveByPrimaryKey(leave);
          // resultCode = 0
          return AMConstants.NORMAL_FINISH;
        } catch (SQLException e) {
          // resultCode = 1
          return AMConstants.ERROR_FINISH;
        }
      }
    }
  }
}
